import request from '@/utils/request'
import * as imageConversion from 'image-conversion'
import randomString from '../z/zfunction/randomString'
// import request from 'axios'
export async function UploadServlet(data) {
  let imgurl=DataURLimg.split(":");
  delete imgurl[imgurl.length-1];
  imgurl=imgurl.join(":")
  imgurl=imgurl.slice(0,-1)
  //获取最后一个.的位置
  let index = data.name.lastIndexOf('.')
  //获取后缀
  let ext = data.name.substr(index + 1)
  return new Promise((resolve, reject) => {
    imageConversion
      .compressAccurately(data, 100)
      .then((res) => {
        let formData = new FormData()
        formData.append('pic', res, `${randomString()}.${ext}`)
        request
          .post(imgurl+'/UploadServlet', formData)
          .then((reqdata) => {
            let { data } = reqdata
            resolve(data)
          })
          .catch((err) => {
            reject(err)
          })
      })
      .catch((err) => {
        reject(err)
      })
  })
}

export default UploadServlet
